package com.party.mobile.biz.crowdfund;

import com.google.common.base.Function;
import com.party.common.constant.Constant;
import com.party.common.paging.Page;
import com.party.common.utils.LangUtils;
import com.party.common.utils.PartyCode;
import com.party.core.exception.BusinessException;
import com.party.core.model.YesNoStatus;
import com.party.core.model.activity.ActivityDetail;
import com.party.core.model.activity.CrowfundResources;
import com.party.core.model.counterfoil.Counterfoil;
import com.party.core.model.crowdfund.CrowdfundEvent;
import com.party.core.model.member.MemberGroup;
import com.party.core.service.activity.IActivityDetailService;
import com.party.core.service.activity.ICrowfundResourcesService;
import com.party.core.service.counterfoil.ICounterfoilService;
import com.party.core.service.crowdfund.ICrowdfundEventService;
import com.party.core.service.member.IMemberGroupService;
import com.party.mobile.web.dto.crowdfund.input.CrowdfundEventHomeInput;
import com.party.mobile.web.dto.crowdfund.output.CrowdfundEventHomeOutput;
import com.party.mobile.web.dto.crowdfund.output.CrowdfundEventOutput;
import org.apache.commons.lang3.StringEscapeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Nullable;
import java.util.List;

/**
 * 众筹主题业务接口
 * Created by wei.li.
 *
 * @date 2018/1/16 0016
 * @time 16:21
 */
@Service
public class CrowdfundEventBizService {

    @Autowired
    private ICrowdfundEventService crowdfundEventService;

    @Autowired
    private IMemberGroupService memberGroupService;

    @Autowired
    private IActivityDetailService activityDetailService;

    @Autowired
    private ICrowfundResourcesService crowfundResourcesService;

    @Autowired
    private ICounterfoilService counterfoilService;

    /**
     * 获取众筹主题详情
     *
     * @param id 编号
     * @return 详情
     */
    public CrowdfundEventOutput detail(String id){
        CrowdfundEvent crowdfundEvent = crowdfundEventService.get(id);
        if (null == crowdfundEvent) {
            throw new BusinessException(PartyCode.IS_NO_DATA, "众筹主题为空");
        }
        CrowdfundEventOutput output = CrowdfundEventOutput.transform(crowdfundEvent);

        //创建者
        MemberGroup member = memberGroupService.get(crowdfundEvent.getCreateBy());
        if (null != member){
            output.setAuthorName(member.getRealname());
            output.setAuthorLogo(member.getLogo());
        }
        output.setAuthorId(crowdfundEvent.getCreateBy());

        //详情
        ActivityDetail activityDetail = activityDetailService.getByRefId(crowdfundEvent.getId());
        if (null != activityDetail){
            output.setContent(StringEscapeUtils.unescapeHtml4(activityDetail.getContent()));
            output.setMatchStandard(StringEscapeUtils.unescapeHtml4(activityDetail.getMatchStandard()));
            output.setApplyRelated(StringEscapeUtils.unescapeHtml4(activityDetail.getApplyRelated()));
        }

        //跑马灯
        List<CrowfundResources> picList = crowfundResourcesService.findByRefId(crowdfundEvent.getId(), "1");
        output.setPicList(picList);

        // 视频
        List<CrowfundResources> videoList = crowfundResourcesService.findByRefId(crowdfundEvent.getId(), "2");
        if (videoList.size() > 0) {
            output.setVideo(videoList.get(0));
        }
        List<Counterfoil> counterfoils = counterfoilService.findByBusinessId(crowdfundEvent.getId(), Constant.BUSINESS_TYPE_CROWDFUND);
        if (counterfoils.size() > 0) {
            Counterfoil counterfoil = counterfoils.get(0);
            output.setPrice(counterfoil.getPayment());
        }
        return output;
    }


    /**
     * 众筹主题主页列表
     * @param page 分页参数
     * @param input 输入参数
     * @return 列表数据
     */
    public List<CrowdfundEventHomeOutput> homeList(Page page, CrowdfundEventHomeInput input){
        CrowdfundEvent crowdfundEvent = new CrowdfundEvent();
        crowdfundEvent.setIsHome(input.getIsHome());
        crowdfundEvent.setProgress(input.getStatus());
        crowdfundEvent.setCreateBy(input.getMemberId());

        List<CrowdfundEvent> list = crowdfundEventService.listPage(crowdfundEvent, page);
        return LangUtils.transform(list, new Function<CrowdfundEvent, CrowdfundEventHomeOutput>() {
            @Nullable
            @Override
            public CrowdfundEventHomeOutput apply(@Nullable CrowdfundEvent event) {
                CrowdfundEventHomeOutput output = CrowdfundEventHomeOutput.transform(event);
                output.setHome(YesNoStatus.YES.getCode().equals(event.getIsHome()));
                output.setAuthorLogo(event.getLogo());
                output.setAuthorName(event.getRealname());
                output.setTitle(event.getName());
                return output;
            }
        });
    }

    /**
     * 主题简单信息
     *
     * @param eventId 主题id
     * @return
     */
    public CrowdfundEventOutput getEventSimpleInfo(String eventId) {
        CrowdfundEvent crowdfundEvent = crowdfundEventService.get(eventId);
        if (null == crowdfundEvent || YesNoStatus.YES.getCode().equals(crowdfundEvent.getDelFlag())) {
            throw new BusinessException(PartyCode.IS_NO_DATA, "主题不存在");
        }

        CrowdfundEventOutput output = CrowdfundEventOutput.transform(crowdfundEvent);
        output.setTitle(crowdfundEvent.getName());
        return output;
    }
}
